package com.chengyu.core.service.pay.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.PeriodRuleParams;
import com.alipay.api.domain.AlipayUserAgreementPageSignModel;
import com.alipay.api.domain.IdentityParams;
import com.alipay.api.domain.AccessParams;
import com.alipay.api.domain.ZmAuthParams;
import com.alipay.api.request.AlipayUserAgreementPageSignRequest;
import com.alipay.api.domain.ProdParams;
import com.alipay.api.response.AlipayUserAgreementPageSignResponse;
import com.alipay.api.domain.DeviceParams;
import com.alipay.api.domain.SubMerchantParams;

import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayUserAgreementPageSign {

    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayUserAgreementPageSignRequest request = new AlipayUserAgreementPageSignRequest();
        AlipayUserAgreementPageSignModel model = new AlipayUserAgreementPageSignModel();

        // 设置请按当前接入的方式进行填充
        AccessParams accessParams = new AccessParams();
        accessParams.setChannel("ALIPAYAPP");
        model.setAccessParams(accessParams);

        // 设置周期管控规则参数period_rule_params
        PeriodRuleParams periodRuleParams = new PeriodRuleParams();
        periodRuleParams.setPeriod(3L);
        periodRuleParams.setTotalAmount("600");
        periodRuleParams.setExecuteTime("2019-01-23");
        periodRuleParams.setSingleAmount("10.99");
        periodRuleParams.setTotalPayments(12L);
        periodRuleParams.setPeriodType("DAY");
        model.setPeriodRuleParams(periodRuleParams);

        // 设置当前用户签约请求的协议有效周期
        model.setSignValidityPeriod("2m");

        // 设置用户在商户网站的登录账号
        model.setExternalLogonId("13852852877");

        // 设置签约第三方主体类型
        model.setThirdPartyType("PARTNER");

        // 设置芝麻授权信息
        ZmAuthParams zmAuthParams = new ZmAuthParams();
        zmAuthParams.setBuckleAppId("1001164");
        zmAuthParams.setBuckleMerchantId("268820000000414397785");
        model.setZmAuthParams(zmAuthParams);

        // 设置商户签约号
        model.setExternalAgreementNo("test");

        // 设置销售产品码
        model.setProductCode("GENERAL_WITHHOLDING");

        // 设置此参数用于传递子商户信息
        SubMerchantParams subMerchant = new SubMerchantParams();
        subMerchant.setSubMerchantName("滴滴出行");
        subMerchant.setSubMerchantServiceName("滴滴出行免密支付");
        subMerchant.setSubMerchantServiceDescription("免密付车费，单次最高500");
        subMerchant.setSubMerchantId("2088123412341234");
        model.setSubMerchant(subMerchant);

        // 设置签约产品属性
        ProdParams prodParams = new ProdParams();
        prodParams.setAuthBizParams("{\"platform\":\"taobao\"}");
        model.setProdParams(prodParams);

        // 设置用户实名信息参数
//        IdentityParams identityParams = new IdentityParams();
//        identityParams.setCertNo("61102619921108888");
//        identityParams.setSignUserId("2088202888530893");
//        identityParams.setUserName("张三");
//        identityParams.setSignOpenId("031_DfFvT0Ufzk1852BLPnhuSWiztu4NqbkO35ylXPow-Y6");
//        identityParams.setIdentityHash("8D969EEF6ECAD3C29A3A629280E686CF0C3F5D5A86AFF3CA12020C923ADC6C92");
//        model.setIdentityParams(identityParams);

        // 设置设备信息参数
        DeviceParams deviceParams = new DeviceParams();
        deviceParams.setDeviceName("电视");
        deviceParams.setDeviceId("device12345");
        deviceParams.setDeviceType("TV");
        model.setDeviceParams(deviceParams);

        // 设置个人签约产品码
        model.setPersonalProductCode("GENERAL_WITHHOLDING_P");

        // 设置商户希望限制的签约用户的年龄范围
        model.setUserAgeRange("{\"min\":\"18\",\"max\":\"30\"}");

        // 设置签约营销参数
        model.setPromoParams("{\"key\":\"value\"}");

        // 设置协议生效类型
        model.setAgreementEffectType("DIRECT");

        // 设置协议签约场景
        model.setSignScene("INDUSTRY|CARRENTAL");

        // 设置签约有效时间限制
//        model.setEffectTime(300L);

        request.setBizModel(model);
//        AlipayUserAgreementPageSignResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
         AlipayUserAgreementPageSignResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCYudUBLLTRh3V/XKApnA4kX6sCJPGbKgobpK5+BPZxtnBVi2rOvPfDsNj7dZsog3c7qYKTWSq4Dmx60bBF3/pTYnU89+qjd3E7gbuvr3EGpx8yiygnU0RR8UNM5pHNUMUgtvpB1keLkxKBlT6n8oNk373vQbF/aQon/dk0NATOe7zAOZui8pezWpL9LCaPyMgb2LOa8iqdSXoldjyty1DKcmkoPLoIfV9kvt0KMtmrsEy0WXZr28ECL7AWYdg1EYoaag5ycCT9w2NOMZYsfRdrd5SDniCY9/CCT1mjlaybiJubP2khB48wnNQwBUZCpDKmhXw7pkzvBcxJapLDmQtDAgMBAAECggEATrokkflwsD3KlL+zlB9srtVd+rn+jYSrNzq59B4sx8tKtt4qTa3dblPgVid2tvil4uxrcfdZ7Q0KYHltC2f8yzUKyEXn/yHYDdu/Wz3t3ocRruVK6pKiujbLAX4tlI/ZPhb0hDGoKcvDk2bSqJJ7pukrKIVNsSb9ZEYXzDAwdhtJAg2NRZ3Z+9GRzMnAJ40Q5UaSA/tpK6EnxplQTHi8T0RD1hTVdpwNEIjm94MOPdv1PLRgrDt8IaEOW+8HDJxLWOaf3AwfKbcyRFo8A9277wPmBsz/WR/7vSPq8as0ZdwUhR1okZxFP9SoY8/2HijDZFxuos0idRnExdPdTnuHMQKBgQDG4L+1z9YkGUy3ioSEBYo7K7KkWzCOUNIGTvOgsfSeQMzzhzbArQ/GwvFU7WdtGxWuyQNiLa3u4mLH/M/zbrAgrpvadCHyb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u7q8mGT+d3vXS6bMLs2XDmXTIFPQ5lJ390f/Du6camYHgpTsT75q9yRnIH9IZl5olM1zF+uRmY/pymtu07geS+wN6JOQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("9021000135631229");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
